package com.lgx.mall.view.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.view.entity.WebPageDetail;
import com.lgx.mall.view.service.IWebPageDetailService;
import com.lgx.right.entity.Permission;
import com.lgx.right.service.IPermissionService;

@Controller
@RequestMapping("/webPage")
public class WebPageDetailController {

	@Autowired
	private IWebPageDetailService webPageDetailService;
	
	@Autowired
	private IPermissionService permissionService;
	
	public IPermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public IWebPageDetailService getWebPageDetailService() {
		return webPageDetailService;
	}

	public void setWebPageDetailService(
			IWebPageDetailService webPageDetailService) {
		this.webPageDetailService = webPageDetailService;
	}
	
	@RequestMapping("/webPageDetailMgr.do")
	public String webPageDetailMgr(HttpServletRequest request,HttpServletResponse response) {
		String categoryId = request.getParameter("categoryId");
		request.setAttribute("categoryId", categoryId);
		Permission p = permissionService.findPermissionById(Long.parseLong(categoryId));
		if(p != null) {
			request.setAttribute("name", p.getName());
			request.setAttribute("parentName", p.getParent().getName());
		}
			return "mall/view/webPage/webPageDetailMgr";
	}
	
	@RequestMapping("/list.do")
	public void list (HttpServletRequest request,HttpServletResponse response) {
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
	
		Object[] params = new Object[10];
		String categoryId = request.getParameter("categoryId");
		params[0] = categoryId;
		
		Pagination page = this.webPageDetailService.findWebPageDetailPagination(params, Integer.parseInt(pageSize),Integer.parseInt(pageNum));
		Json2Utils.parseJSON(page, response);
	}
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response) {
		String type = request.getParameter("type");
		String categoryId = request.getParameter("categoryId");
		String id = request.getParameter("id");
		
		if("edit".equals(type)) {
			WebPageDetail webPageDetail = this.webPageDetailService.findWebPageDetailById(Long.parseLong(id));
			request.setAttribute("webPageDetail", webPageDetail);
		}
		List<Dictionary> types = DictUtils.getDictListByKind(DictUtils.FIRST_TYPE);
		request.setAttribute("types", types);
	
		request.setAttribute("categoryId", categoryId);
		request.setAttribute("id", id);
		request.setAttribute("type", type);
		return "mall/view/webPage/webPageDetail-init";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response) {
		String categoryId = request.getParameter("categoryId");
		String type = request.getParameter("types");
		if(!StringUtils.isEmpty(type))type = EscapeUtils.unescape(type);	
		String entityId = request.getParameter("entityId");
		if(!StringUtils.isEmpty(entityId))entityId = EscapeUtils.unescape(entityId);	
		String entityName = request.getParameter("entityName");
		if(!StringUtils.isEmpty(entityName))entityName = EscapeUtils.unescape(entityName);	
		String entityUrl = request.getParameter("entityUrl");
		if(!StringUtils.isEmpty(entityUrl))entityUrl = EscapeUtils.unescape(entityUrl);	
		String imageUrl1 = request.getParameter("imageUrl1");
		if(!StringUtils.isEmpty(imageUrl1))imageUrl1 = EscapeUtils.unescape(imageUrl1);	
		String imageUrl2 = request.getParameter("imageUrl2");
		if(!StringUtils.isEmpty(imageUrl2))imageUrl2 = EscapeUtils.unescape(imageUrl2);
		
		WebPageDetail webPageDetail = new WebPageDetail();
		
		webPageDetail.setCategoryId(Long.parseLong(categoryId));
		webPageDetail.setEntityId(entityId);
		webPageDetail.setEntityName(entityName);
		webPageDetail.setEntityUrl(entityUrl);
		webPageDetail.setIsDelete("0");
		webPageDetail.setImageUrl2(imageUrl2);
		webPageDetail.setType(Integer.parseInt(type));
		webPageDetail.setImageUrl1(imageUrl1);
				
		try {
			this.webPageDetailService.createWebPageDetail(webPageDetail);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}	
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		WebPageDetail webPageDetail = this.webPageDetailService.findWebPageDetailById(Long.parseLong(id));
		String categoryId = request.getParameter("categoryId");
		
		String type = request.getParameter("types");
		if(!StringUtils.isEmpty(type))type = EscapeUtils.unescape(type);	
		String entityId = request.getParameter("entityId");
		if(!StringUtils.isEmpty(entityId))entityId = EscapeUtils.unescape(entityId);	
		String entityName = request.getParameter("entityName");
		if(!StringUtils.isEmpty(entityName))entityName = EscapeUtils.unescape(entityName);	
		String entityUrl = request.getParameter("entityUrl");
		if(!StringUtils.isEmpty(entityUrl))entityUrl = EscapeUtils.unescape(entityUrl);	
		String imageUrl1 = request.getParameter("imageUrl1");
		if(!StringUtils.isEmpty(imageUrl1))imageUrl1 = EscapeUtils.unescape(imageUrl1);	
		String imageUrl2 = request.getParameter("imageUrl2");
		if(!StringUtils.isEmpty(imageUrl2))imageUrl2 = EscapeUtils.unescape(imageUrl2);
		
		webPageDetail.setCategoryId(Long.parseLong(categoryId));
		webPageDetail.setEntityId(entityId);
		webPageDetail.setEntityName(entityName);
		webPageDetail.setEntityUrl(entityUrl);
		webPageDetail.setImageUrl2(imageUrl2);
		webPageDetail.setType(Integer.parseInt(type));
		webPageDetail.setImageUrl1(imageUrl1);
					
		try {
			this.webPageDetailService.updateWebPageDetail(webPageDetail);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
	}
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response) {
		
		String ids = request.getParameter("ids");
		try {
			this.webPageDetailService.deleteWebPageDetails(ids.split(","));
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
}
